<?php

namespace App\Service\Cruise\CruiseProject;

use App\Enums\BaseErrorCode;
use App\Exceptions\Custom\ResponseHttpException;
use App\Models\Cruise\CruiseProjectCategoryModel;
use App\Models\Cruise\CruiseTaskProjectModel;
use App\Service\BaseService;

class CruiseCategoryService
{

    /**
     * @return array
     */
    public static function list(array $types): array
    {
        $lists = CruiseProjectCategoryModel::query()
            ->with([
                'childRel' => function ($query) {
                    $query->select('id', 'pid', 'name', 'sort')->orderByDesc('sort');
                }
            ])
            ->where('pid', 0)
            ->whereIn('type', $types)
            ->select('id', 'name', 'pid', 'sort', 'type')
            ->orderByDesc('sort')
            ->get();
        return ['lists' => $lists];
    }

    /**
     * 修改（编辑）分类
     * @param $id
     * @param $name
     * @param $pid
     * @return void
     */
    public static function update($id, $name, $pid)
    {
        $cruiserCategoryModel = CruiseProjectCategoryModel::query()
            ->withoutGlobalScope('isDel')
            ->find($id);
        $cruiserCategoryModel->name      = $name;
        $cruiserCategoryModel->pid       = $pid;
        $cruiserCategoryModel->isDeleted = 0;
        $cruiserCategoryModel->save();
    }

    /**
     * 添加分类
     * @param $id
     * @param $name
     * @return void
     */
    public static function add($id, $name, $type)
    {
        if ($id) {
            $category = CruiseProjectCategoryModel::query()->find($id);
            if (!$category) {
                BaseService::throwErr("不存在的分类id");
            }

            $pid = $category->pid;
            if ($pid) {
                throw new ResponseHttpException(BaseErrorCode::$ERROR_OCCURRED, null, "暂不支持三级分类的添加");
            }

            $params = [
                'pid'  => $id,
                'name' => $name,
                'type' => $category->type,
            ];
        } else {
            $params = [
                'pid'  => 0,
                'name' => $name,
                'type' => $type,
            ];
        }
        $cruiseCategoryModel = CruiseProjectCategoryModel::query()->withoutGlobalScope('isDel')->firstOrNew($params);
        $cruiseCategoryModel->isDeleted = 0;
        if (!$cruiseCategoryModel->id) {
            $cruiseCategoryModel->id = CruiseBaseService::popId(new CruiseProjectCategoryModel);
        }
        $cruiseCategoryModel->save();
    }

    /**
     * 删除对应分类
     * @param $id
     * @return bool
     */
    public static function delete($id)
    {
        $projectCount = CruiseTaskProjectModel::query()
            ->where('categoryId', $id)
            ->count();
        $categoryCount = CruiseProjectCategoryModel::query()
            ->where('pid', $id)
            ->count();

        if (!$projectCount && !$categoryCount) {
            CruiseProjectCategoryModel::query()
                ->where('id', $id)
                ->update([
                    'isDeleted' => 1
                ]);
            return false;
        }
        return true;
    }
}
